package jxta.chat.entities;


import jxta.chat.util.Constants;
import jxta.chat.util.Constants.ChatInvitaionResponseVals;
import jxta.test.JxtaManager;
import net.jxta.endpoint.Message;
import net.jxta.endpoint.MessageElement;
import net.jxta.endpoint.StringMessageElement;

/**
 * A class that creates voice chat invitation messages messages
 *
 */
public class VoiceChatMessage extends AbstractMessage{

	private JxtaManager manager;
	
	public VoiceChatMessage(){
		
	}
	
	public VoiceChatMessage(Message msg){
		super(msg);
	}
	
	public VoiceChatMessage(Message msg, JxtaManager manager){
		super(msg);
		
		this.manager = manager;
	}
	
	public Message createMessage(MessageParams params){
		Message retVal = super.createMessage(params);
		
		// Search for request param first, if it's not found than we'll search for the response param
		Object param = params.getParameter(Constants.MessageParamsKeys.REQUEST) != null ? params.getParameter(Constants.MessageParamsKeys.REQUEST) : null;
		
		if (param != null){
			MessageElement me = new StringMessageElement(Constants.CHAT_REQUEST_ATTRIBUTE, param.toString(), null);
			retVal.addMessageElement(me);			
		}
		else{
			param = params.getParameter(Constants.MessageParamsKeys.RESPONSE) != null ? params.getParameter(Constants.MessageParamsKeys.RESPONSE) : null;
			
			if (param != null){
				MessageElement me = new StringMessageElement(Constants.CHAT_RESPONSE_ATTRIBUTE, param.toString(), null);
				retVal.addMessageElement(me);
			}		
		}
		
		return msg = retVal;
	}
	
	public String getMessageType(){
		return Constants.MessageTypes.VOIP_CHAT.getName();
	}

	@Override
	public Object getAttributeValue(String attribute){
		
		ChatInvitaionResponseVals retVal = null; 
		
		if (attribute.equalsIgnoreCase(Constants.CHAT_RESPONSE_ATTRIBUTE)){
			retVal = msg.getMessageElement(attribute) != null ? ChatInvitaionResponseVals.getValue(msg.getMessageElement(attribute).toString()) : null;
		}
		else{		
			return super.getAttributeValue(attribute);
		}
		
		return retVal;
	}
}
